<?php
$dl_id=$_GET['id'];
$file_type=$_GET['t']==''?'':('_'.$_GET['t']);

include_once "include/class.php";
$db=new data();
if (!isset($_SESSION['yaoyoa_user'])||!is_array($_SESSION['yaoyoa_user'])){
	header();
	die('error');
}
if (!is_numeric($dl_id)){
	$encrypt=new encrypt();
	$dl_id=$encrypt->decrypt(urldecode($dl_id));
}
if (!is_numeric($dl_id)){
	header("HTTP/1.0 404 Bad Request");
	echo "No such file!";
	exit();
}
$result=$db->get('file',$dl_id);
if ($result->num_rows<=0){
	header("HTTP/1.0 404 Bad Request");
	echo "No such file!";
	exit();
}
$des_row = $result->fetch_assoc ();

$file_dir=$_SERVER['DOCUMENT_ROOT'].$des_row['dir'].$des_row['name'].$file_type.'.'.$des_row['file_type'];
if ($file_dir=='' || !file_exists($file_dir)) {
    if ($file_type=='_b'){
        if(file_exists($_SERVER['DOCUMENT_ROOT'].$des_row['dir'].$des_row['name'].'.'.$des_row['file_type'])){
            $file_dir=$_SERVER['DOCUMENT_ROOT'].$des_row['dir'].$des_row['name'].'.'.$des_row['file_type'];
        }
        else{
            echo "No such file!";
            exit();
        }
    }
}
if (file_exists($file_dir)) {
    header('Content-Description: File Transfer');
//    header("Content-Type: application/force-download"); 
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file_dir));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file_dir));
    ob_clean();
    flush();
    readfile($file_dir);
    exit;
}
?>
